﻿<UserControl x:Class="WpfFundamentals.Controls.SelectionCombo"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:converter="clr-namespace:WpfFundamentals.Converter"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:own="clr-namespace:WpfFundamentals.Controls"
             d:DesignHeight="20"
             d:DesignWidth="100"
             mc:Ignorable="d">

    <UserControl.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="pack://application:,,,/WpfFundamentals;component/Themes/Button.Generic.xaml" />
            </ResourceDictionary.MergedDictionaries>

            <converter:BoolConverter x:Key="BoolConverter"
                                     FalseValue="Collapsed"
                                     TrueValue="Visible" />

            <!--  Brushes  -->
            <LinearGradientBrush x:Key="NormalBrush" StartPoint="0,0" EndPoint="0,1">
                <GradientBrush.GradientStops>
                    <GradientStopCollection>
                        <GradientStop Offset="0.0" Color="#FFF" />
                        <GradientStop Offset="1.0" Color="#CCC" />
                    </GradientStopCollection>
                </GradientBrush.GradientStops>
            </LinearGradientBrush>

            <LinearGradientBrush x:Key="NormalBorderBrush" StartPoint="0,0" EndPoint="0,1">
                <GradientBrush.GradientStops>
                    <GradientStopCollection>
                        <GradientStop Offset="0.0" Color="#CCC" />
                        <GradientStop Offset="1.0" Color="#444" />
                    </GradientStopCollection>
                </GradientBrush.GradientStops>
            </LinearGradientBrush>

            <SolidColorBrush x:Key="GlyphBrush" Color="#444" />

            <SolidColorBrush x:Key="PopupBorderBrush" Color="#FF888888" />

            <LinearGradientBrush x:Key="DarkBrush" StartPoint="0,0" EndPoint="0,1">
                <GradientBrush.GradientStops>
                    <GradientStopCollection>
                        <GradientStop Offset="0.0" Color="#FFF" />
                        <GradientStop Offset="1.0" Color="#AAA" />
                    </GradientStopCollection>
                </GradientBrush.GradientStops>
            </LinearGradientBrush>

            <LinearGradientBrush x:Key="PressedBrush" StartPoint="0,0" EndPoint="0,1">
                <GradientBrush.GradientStops>
                    <GradientStopCollection>
                        <GradientStop Offset="0.0" Color="#BBB" />
                        <GradientStop Offset="0.1" Color="#EEE" />
                        <GradientStop Offset="0.9" Color="#EEE" />
                        <GradientStop Offset="1.0" Color="#FFF" />
                    </GradientStopCollection>
                </GradientBrush.GradientStops>
            </LinearGradientBrush>

            <SolidColorBrush x:Key="DisabledForegroundBrush" Color="#888" />

            <SolidColorBrush x:Key="DisabledBackgroundBrush" Color="#EEE" />

            <SolidColorBrush x:Key="WindowBackgroundBrush" Color="#FFF" />

            <!--  <SolidColorBrush x:Key="SelectedBackgroundBrush" Color="#DDD" />  -->

            <!--  Brushes end  -->

            <Style TargetType="{x:Type ComboBoxItem}">
                <Setter Property="OverridesDefaultStyle" Value="true" />
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type ComboBoxItem}">
                            <Button HorizontalContentAlignment="Stretch"
                                    VerticalContentAlignment="Stretch"
                                    Padding="0"
                                    Style="{StaticResource TransparentButtonStyle}">
                                <Button.Content>
                                    <ContentPresenter />
                                </Button.Content>
                            </Button>
                            <ControlTemplate.Triggers>
                                <Trigger Property="IsEnabled" Value="false">
                                    <Setter Property="Foreground" Value="{StaticResource DisabledForegroundBrush}" />
                                </Trigger>
                            </ControlTemplate.Triggers>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>

            <ControlTemplate x:Key="ComboBoxToggleButton" TargetType="{x:Type ToggleButton}">
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition />
                        <ColumnDefinition Width="20" />
                    </Grid.ColumnDefinitions>
                    <Border x:Name="Border"
                            Grid.ColumnSpan="2"
                            Background="{StaticResource NormalBrush}"
                            BorderBrush="{StaticResource NormalBorderBrush}"
                            BorderThickness="1"
                            CornerRadius="2" />
                    <Border Grid.Column="0"
                            Margin="1"
                            Background="{StaticResource WindowBackgroundBrush}"
                            BorderBrush="{StaticResource NormalBorderBrush}"
                            BorderThickness="0,0,1,0"
                            CornerRadius="2,0,0,2" />
                    <Path x:Name="Arrow"
                          Grid.Column="1"
                          HorizontalAlignment="Center"
                          VerticalAlignment="Center"
                          Data="M 0 0 L 4 4 L 8 0 Z"
                          Fill="{StaticResource GlyphBrush}" />
                </Grid>
                <ControlTemplate.Triggers>
                    <Trigger Property="ToggleButton.IsMouseOver" Value="true">
                        <Setter TargetName="Border" Property="Background" Value="{StaticResource DarkBrush}" />
                    </Trigger>
                    <Trigger Property="ToggleButton.IsChecked" Value="true">
                        <Setter TargetName="Border" Property="Background" Value="{StaticResource PressedBrush}" />
                        <Setter TargetName="Arrow" Property="Data" Value="M 0 4 L 4 0 L 8 4" />
                    </Trigger>
                    <Trigger Property="IsEnabled" Value="False">
                        <Setter TargetName="Border" Property="Background" Value="{StaticResource DisabledBackgroundBrush}" />
                        <Setter Property="Foreground" Value="{StaticResource DisabledForegroundBrush}" />
                        <Setter TargetName="Arrow" Property="Fill" Value="{StaticResource DisabledForegroundBrush}" />
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>


            <DataTemplate x:Key="SelectionItemTemplate">
                <StackPanel>
                    <TreeViewItem Name="ExpandableNode"
                                  own:SelectionComboBindingManager.IsExpandedNodeMemberPath="{Binding Path=IsExpandedNodeMemberPath, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=UserControl}}"
                                  own:SelectionComboBindingManager.VisibilityBindingForTreeViewItem="{Binding Path=ChildrenNodeMemberPath, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=UserControl}}"
                                  Visibility="Collapsed">
                        <TreeViewItem.Header>
                            <StackPanel Margin="1,1,1,1" Orientation="Horizontal">
                                <CheckBox HorizontalAlignment="Left"
                                          VerticalAlignment="Center"
                                          IsThreeState="True"
                                          own:SelectionComboBindingManager.IsCheckBoxVisibleMemberPath="{Binding Path=IsCheckBoxVisibleMemberPath, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=UserControl}}"
                                          own:SelectionComboBindingManager.IsEnabledMemberPath="{Binding Path=IsEnabledMemberPath, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=UserControl}}"
                                          own:SelectionComboBindingManager.IsSelectedMemberPath="{Binding Path=IsSelectedMemberPath, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=UserControl}}" />
                                <ContentControl VerticalAlignment="Center"
                                                own:SelectionComboBindingManager.Content="{Binding}"
                                                own:SelectionComboBindingManager.DisplayMemberPath="{Binding Path=DisplayMemberPath, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=UserControl}}"
                                                own:SelectionComboBindingManager.ItemTemplate="{Binding Path=ItemTemplate, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=UserControl}}" />
                            </StackPanel>
                        </TreeViewItem.Header>
                        <ItemsControl ItemTemplate="{DynamicResource SelectionItemTemplate}" own:SelectionComboBindingManager.ItemsSource="{Binding Path=ChildrenNodeMemberPath, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=UserControl}}" />
                    </TreeViewItem>
                    <StackPanel Name="LowestLevelNode"
                                Margin="3,1,1,1"
                                Orientation="Horizontal"
                                own:SelectionComboBindingManager.VisibilityBindingForStackPanel="{Binding Path=ChildrenNodeMemberPath, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=UserControl}}">
                        <CheckBox HorizontalAlignment="Left"
                                  VerticalAlignment="Center"
                                  IsThreeState="False"
                                  own:SelectionComboBindingManager.IsEnabledMemberPath="{Binding Path=IsEnabledMemberPath, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=UserControl}}"
                                  own:SelectionComboBindingManager.IsSelectedMemberPath="{Binding Path=IsSelectedMemberPath, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=UserControl}}">
                            <ContentControl VerticalAlignment="Center"
                                            own:SelectionComboBindingManager.Content="{Binding}"
                                            own:SelectionComboBindingManager.DisplayMemberPath="{Binding Path=DisplayMemberPath, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=UserControl}}"
                                            own:SelectionComboBindingManager.ItemTemplate="{Binding Path=ItemTemplate, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=UserControl}}" />
                        </CheckBox>
                    </StackPanel>
                </StackPanel>
            </DataTemplate>

        </ResourceDictionary>
    </UserControl.Resources>

    <Grid>
        <ComboBox Name="InternalComboBox"
                  HorizontalContentAlignment="Stretch"
                  VerticalContentAlignment="Stretch"
                  DataContext="{Binding Path=DataContext, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=UserControl}}"
                  DropDownClosed="InternalComboBox_DropDownClosed"
                  IsEditable="{Binding Path=IsEditable, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=UserControl}}"
                  IsEnabled="{Binding Path=CanExecute,  RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=UserControl}}"
                  IsSynchronizedWithCurrentItem="True"
                  IsTabStop="{Binding Path=IsTabStop, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=UserControl}}"
                  ItemsSource="{Binding Path=ItemsSource, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=UserControl}}"
                  ItemTemplate="{StaticResource SelectionItemTemplate}"
                  OverridesDefaultStyle="True"
                  PreviewKeyUp="InternalComboBox_PreviewKeyUp"
                  ScrollViewer.CanContentScroll="False"
                  ScrollViewer.HorizontalScrollBarVisibility="Auto"
                  ScrollViewer.VerticalScrollBarVisibility="Auto"
                  VirtualizingStackPanel.IsVirtualizing="True"
                  VirtualizingStackPanel.VirtualizationMode="Recycling">
            <ComboBox.Template>
                <ControlTemplate TargetType="{x:Type ComboBox}">
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="*" />
                            <ColumnDefinition Width="22" />
                        </Grid.ColumnDefinitions>
                        <ToggleButton Name="ToggleButton"
                                      Grid.Column="0"
                                      Grid.ColumnSpan="2"
                                      ClickMode="Press"
                                      Focusable="false"
                                      IsChecked="{Binding Path=IsDropDownOpen, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}"
                                      Template="{StaticResource ComboBoxToggleButton}" />
                        <Border Name="PresenterBorder">
                            <ContentPresenter x:Name="Presenter"
                                              Grid.Column="0"
                                              Margin="3,3,0,3"
                                              HorizontalAlignment="Left"
                                              VerticalAlignment="Center"
                                              IsHitTestVisible="False">
                                <ContentPresenter.Content>
                                    <DockPanel>
                                        <TextBlock Name="TextBlockDisplayValues"
                                                   Text="{Binding Path=Text, Mode=TwoWay, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=UserControl}}"
                                                   TextTrimming="CharacterEllipsis" />
                                        <TextBox Name="TextBoxDisplayValues"
                                                 Width="{Binding ElementName=PresenterBorder,
                                                                 Path=ActualWidth}"
                                                 KeyUp="TextBoxDisplayValues_KeyUp"
                                                 LostFocus="TextBoxDisplayValues_LostFocus"
                                                 Text="{Binding Path=Text,
                                                                Mode=TwoWay,
                                                                RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=UserControl},
                                                                UpdateSourceTrigger=PropertyChanged}"
                                                 Visibility="Collapsed" />
                                    </DockPanel>
                                </ContentPresenter.Content>
                            </ContentPresenter>
                        </Border>
                        <Popup Name="Popup"
                               AllowsTransparency="True"
                               Focusable="False"
                               IsOpen="{TemplateBinding IsDropDownOpen}"
                               Placement="Bottom"
                               PopupAnimation="Slide"
                               StaysOpen="False">
                            <Border Background="{StaticResource WindowBackgroundBrush}"
                                    BorderBrush="{StaticResource PopupBorderBrush}"
                                    BorderThickness="1"
                                    CornerRadius="4">
                                <Grid Name="DropDown"
                                      MinWidth="{TemplateBinding ActualWidth}"
                                      MaxHeight="{TemplateBinding MaxDropDownHeight}"
                                      SnapsToDevicePixels="True">
                                    <Grid>
                                        <Grid.RowDefinitions>
                                            <RowDefinition Height="Auto" />
                                            <RowDefinition Height="*" />
                                        </Grid.RowDefinitions>
                                        <CheckBox Name="SelectAll"
                                                  Grid.Row="0"
                                                  Margin="3,2,2,2"
                                                  Click="SelectAll_Click"
                                                  Content="{Binding Path=SelectAllText, Mode=OneWay, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=UserControl}}"
                                                  IsChecked="{Binding Path=SelectAllIsChecked, Mode=TwoWay, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=UserControl}}"
                                                  Visibility="{Binding Path=HasSelectAll, Converter={StaticResource BoolConverter}, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=UserControl}}" />
                                        <ScrollViewer Grid.Row="1"
                                                      CanContentScroll="{Binding Path=CanContentScroll, RelativeSource={RelativeSource Mode=FindAncestor,  AncestorType=UserControl}}"
                                                      VirtualizingStackPanel.IsVirtualizing="True"
                                                      VirtualizingStackPanel.VirtualizationMode="Recycling">
                                            <VirtualizingStackPanel IsItemsHost="True" KeyboardNavigation.DirectionalNavigation="Contained" />
                                        </ScrollViewer>
                                    </Grid>
                                </Grid>
                            </Border>
                        </Popup>
                    </Grid>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsEnabled" Value="false">
                            <Setter Property="Foreground" Value="{StaticResource DisabledForegroundBrush}" />
                        </Trigger>
                        <Trigger Property="IsEditable" Value="true">
                            <Setter TargetName="TextBoxDisplayValues" Property="Visibility" Value="Visible" />
                            <Setter TargetName="TextBlockDisplayValues" Property="Visibility" Value="Collapsed" />
                            <Setter TargetName="Presenter" Property="IsHitTestVisible" Value="True" />
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </ComboBox.Template>
        </ComboBox>
    </Grid>

</UserControl>
